<template>
  <div v-show="support" ref="drop" class="uploader-drop" :class="dropClass">
    <i class="el-icon-upload uploader-drop_icon" />
    <div class="uploader-drop_text">
      将文件拖拽至此处，或
      <em>
        <uploader-btn :single="singleFlag">点击上传文件</uploader-btn>
      </em>
    </div>
    <!-- <div class="uploader-drop_text">将文件拖拽至此处，或<em><uploader-btn>点击上传文件</uploader-btn></em>或<em><uploader-btn :directory="true">点击上传文件夹</uploader-btn></em></div> -->
  </div>
</template>

<script>
import { uploaderMixin, supportMixin } from '../common/mixins'
import UploaderBtn from './btn.vue'

const COMPONENT_NAME = 'uploader-drop'

export default {
  name: COMPONENT_NAME,
  components: {
    UploaderBtn
  },
  mixins: [uploaderMixin, supportMixin],
  props: {

    singleFlag: {
      type: Boolean,
      default: false
    }

  },
  data() {
    return {
      dropClass: ''
    }
  },
  mounted() {
    this.$nextTick(() => {
      const dropEle = this.$refs.drop
      const uploader = this.uploader.uploader
      uploader.assignDrop(dropEle)
      uploader.on('dragenter', this.onDragEnter)
      uploader.on('dragleave', this.onDragLeave)
      uploader.on('drop', this.onDrop)
    })
  },
  beforeDestroy() {
    const dropEle = this.$refs.drop
    const uploader = this.uploader.uploader
    uploader.off('dragenter', this.onDragEnter)
    uploader.off('dragleave', this.onDragLeave)
    uploader.off('drop', this.onDrop)
    uploader.unAssignDrop(dropEle)
  },
  methods: {
    onDragEnter() {
      this.dropClass = 'uploader-dragover'
    },
    onDragLeave() {
      this.dropClass = ''
    },
    onDrop() {
      this.dropClass = 'uploader-droped'
    }
  }
}
</script>

<style lang="scss" scoped>
.uploader-drop {
  background-color: #fff;
  box-sizing: border-box;
  height: 120px;
  text-align: center;
  position: relative;
  overflow: hidden;
  .uploader-drop_icon {
    font-size: 67px;
    color: #c0c4cc;
    line-height: 70px;
  }
  .uploader-drop_text {
    color: #606266;
    font-size: 14px;
    text-align: center;
    line-height: 50px;
    em label {
      color: #1890ff;
      font-style: normal;
      cursor: pointer;
    }
  }
}
.uploader-dragover {
  border-color: #999;
  background-color: #f7f7f7;
}
</style>
